// pages/my/service-order/service-order.js
const controllers = require('../../controllers/controllerOrder').controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navlist: ['全部', '待取货', '已完成', '待拼团', '已关闭'],
    activenav: 0,
    orderList: [],
    imgurl: '',
    navchoice: '',
    navall: false,
    sales: [],
    salesname: [],
    isshow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    singlesales: '',
    singleordno: '',
    singlesalesname: '',
    pageNumber: 1,
    pageSize: 10,
    isEnd: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  changenav(e) {
    this.setData({
      activenav: e.currentTarget.id
    })
    if (e.currentTarget.id == 0) {
      this.setData({
        navall: true,
        orderList: [],
        navchoice: '',
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else if (e.currentTarget.id == 1) {
      this.setData({
        navchoice: 2,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else if (e.currentTarget.id == 2) {
      this.setData({
        navchoice: 9,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else if (e.currentTarget.id == 3) {
      this.setData({
        navchoice: 1,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else {
      this.setData({
        navchoice: 6,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    }
  },
  //按钮事件
  tapDialogButton(e) {
    console.log(e);
    if (e.detail.index == 0) {
      this.setData({
        isshow: false
      })
    } else {
        //提交
        controllers.serviceComplete({
          orderNo: this.data.singleordno,
          shopId: wx.getStorageSync('shopId'),
          shopAccountId: this.data.singlesales,
          type: 1
        }).then(res => {
          console.log(res);
          this.setData({
            isshow: false,
            pageNumber:1,
            pageSize:10,
            isEnd:false,
            orderList:[]

          })
          this.orderList(this.data.navchoice)
          //获取商品订单列表
          // controllers.orderList({
          //   pageNumber: 1,
          //   pageSize: 20,
          //   shopId: wx.getStorageSync('shopId'),
          //   type: 1,
          //   state: 2
          // }).then(res => {
          //   console.log(res);
          //   if (res.data.list.length != 0) {
          //     this.setData({
          //       orderList: res.data.list
          //     })
          //   }
          // })
          
        })
    }
  },
  //选择销售
  choice(e) {
    console.log(e);

    this.setData({
      singlesales: this.data.sales[e.detail.value].id,
      singlesalesname: this.data.sales[e.detail.value].nick
    })
  },
  //打开选择销售弹框
  toChoiceSales(e) {
    this.setData({
      isshow: true,
      singleordno: e.currentTarget.id
    })
  },


  //跳转到订单详情页
  toOrderDetail(e) {
    wx.navigateTo({
      url: '/pages/order/productOrder-detail/productOrder-detail?id=' + e.currentTarget.id + '&state=' + e.currentTarget.dataset.state
    })
  },
  //获取订单列表
  orderList(state) {
    if (wx.getStorageSync('diantype') == 1) {
      //获取商品订单列表
      controllers.orderList({
        pageNumber: this.data.pageNumber,
        pageSize: this.data.pageSize,
        shopId: wx.getStorageSync('shopId'),
        type: 1,
        state: state || ''
      }).then(res => {
        console.log(res);
        if (res.data.list.length != 0) {
          this.setData({
            orderList: this.data.orderList.concat(res.data.list)
          })
        }
        if (res.data.lastPage) {
          this.setData({
            isEnd: true
          })
        }
      })
    }else{
      controllers.staffPage({
        pageNumber: this.data.pageNumber,
        pageSize: this.data.pageSize,
        shopId: wx.getStorageSync('shopId'),
        accountId:wx.getStorageSync('accountId'),
        shopAccountId:wx.getStorageSync('shopAccountId'),
        type:1,
        state:state || ''
      }).then(res =>{
        console.log(res);
        if (res.data.list.length != 0) {
          this.setData({
            orderList: this.data.orderList.concat(res.data.list)
          })
        }
        if (res.data.lastPage) {
          this.setData({
            isEnd: true
          })
        }
      })
    }

  },
  onLoad: function (options) {
    this.setData({
      imgurl: app.globalData.IMG_URL,
      sales: wx.getStorageSync('sales')
    })
    let salesname = []
    let sales = wx.getStorageSync('sales')
    sales.forEach(function (item, index) {
      salesname.push(item.nick)
    })
    this.setData({
      salesname: salesname
    })

    if (options.id == 0) {
      this.setData({
        navall: true,
        activenav: 0
      })
    } else if (options.id == 1) {
      this.setData({
        navchoice: 2,
        activenav: 1
      })
    } else if (options.id == 2) {
      this.setData({
        navchoice: 9,
        activenav: 2
      })
    } else {
      this.setData({
        navchoice: 1,
        activenav: 3,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      orderList: [],
      isEnd: false,
      pageNumber: 1,
    })
    this.orderList(this.data.navchoice)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      });
      this.orderList(this.data.navchoice)
    }
  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})